package com.cn.medicalcare.controller.backstageservlet.superadmin;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Admin;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.Page;
import com.cn.medicalcare.service.AdminService;
import com.cn.medicalcare.service.serviceimpl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/backstage/superadmin/getadmininpageby_.json")
public class AdminInPageServlet extends HttpServlet {

    private AdminService adminservice = AdminServiceImpl.getInstance();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String role = req.getParameter("role");
        String survival = req.getParameter("survival");
        Admin admin = new Admin().setUsername(username).setRole(role);

        PrintWriter pw = resp.getWriter();
        Map<String, Object> map = new LinkedHashMap<>();
        try {
            if (survival != null && survival.length() > 0) {
                admin.setSurvival(Integer.valueOf(survival));
            }
            int pagenum = Integer.valueOf(req.getParameter("page"));
            int limit = Integer.valueOf(req.getParameter("limit"));
            Page page = null;
            if (pagenum > 0 && limit > 0)
                page = new Page((pagenum - 1) * limit, limit);
            AllPage<Admin> allPage = new AllPage<>(admin, page);
            map.put("status", 200);
            map.put("data", adminservice.selectAdminInPage(allPage));
            map.put("count", adminservice.selectAdminCount(admin));
        } catch (Exception e) {
            map.put("status", 500);
        }
        pw.print(JsonTool.toJson(map));
    }
}
